home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / hydra / misc.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  15KB  |  608 lines

  1. /*============================================================================
  2.                               HydraCom Version 1.00
  3. =============================================================================*/
  4.  
  5. #include "hydracom.h"
  6.  
  7. #include <dos/dosextens.h>
  8. #include <clib/dos_protos.h>
  9.  
  10. static char *chatstart = "\007\007 * Chat mode start\r\n";
  11. static char *chatend   = "\007\007\r\n * Chat mode end\r\n";
  12. static char *chattime  = "\007\007\r\n * Chat mode end - timeout\r\n";
  13.  
  14.  
  15. static void loc_puts (char *s)
  16. {
  17. #ifdef AMIGA
  18.     ConPrintf(LocalRequest,s);
  19. #else
  20.         while (*s) {
  21.               if (*s == '\007')
  22. #if WIN_AGL
  23.                  (void) win_bell();
  24. #else
  25.                  putc(7,stderr);
  26. #endif
  27.               else
  28. #if WIN_AGL
  29.                  win_putc(local_win,*s);
  30. #else
  31.                  putch(*s);
  32. #endif
  33.               s++;
  34.         }
  35. #endif    /* AMIGA */
  36. }/*loc_puts()*/
  37.  
  38.  
  39. int keyabort (void)
  40. {
  41. #define CHATLEN 256
  42.         static byte     chatbuf1[CHATLEN + 5],
  43.                         chatbuf2[CHATLEN + 5],
  44.                        *curbuf = chatbuf1;
  45.         static boolean  warned = false;
  46.         boolean         esc = false;
  47.         char           *p;
  48.         word            c;
  49.  
  50.         if (chattimer > 0L) {
  51.            if (time(NULL) > chattimer) {
  52.               chattimer = lasttimer = 0L;
  53.               hydra_devsend("CON",(byte *) chattime,strlen(chattime));
  54.               loc_puts(&chattime[2]);
  55.            }
  56.            else if ((time(NULL) + 10L) > chattimer && !warned) {
  57.               loc_puts("\007\r\n * Warning: chat mode timeout in 10 seconds\r\n");
  58.               warned = true;
  59.            }
  60.         }
  61.         else if (chattimer != lasttimer) {
  62.            if (chattimer ==  0L) {
  63.               if (nobell) p = " * Remote has chat facility with bell disabled\n";
  64.               else        p = " * Remote has chat facility with bell enabled\n";
  65.               hydra_devsend("CON",(byte *) p,(int) strlen(p));
  66.               loc_puts(" * Hydra session in progress, chat facility now available\r\n");
  67.            }
  68.            else if (chattimer == -1L)
  69.               loc_puts(" * Hydra session in init state, can't chat yet\r\n");
  70.            else if (chattimer == -2L)
  71.               loc_puts(" * Remote has no chat facility available\r\n");
  72.            else if (chattimer == -3L) {
  73.               if (lasttimer > 0L) loc_puts("\r\n");
  74.               loc_puts(" * Hydra session in exit state, can't chat anymore\r\n");
  75.            }
  76.            lasttimer = chattimer;
  77.         }
  78.  
  79. #if WIN_AGL
  80.         while (win_keyscan()) {
  81. #else
  82. #ifdef AMIGA
  83.         while (ConScanKey()) {
  84. #else
  85.         while (kbhit()) {
  86. #endif
  87. #endif
  88.               switch (c = get_key()) {
  89.                      case Esc:
  90.                           esc = true;
  91.                           break;
  92.  
  93.                      case Alt_C:
  94.                           if (chattimer == 0L) {
  95.                              hydra_devsend("CON",(byte *) chatstart,strlen(chatstart));
  96.                              loc_puts(&chatstart[2]);
  97.                              chattimer = lasttimer = time(NULL) + CHAT_TIMEOUT;
  98.                           }
  99.                           else if (chattimer > 0L) {
  100.                              chattimer = lasttimer = 0L;
  101.                              hydra_devsend("CON",(byte *) chatend,strlen(chatend));
  102.                              loc_puts(&chatend[2]);
  103.                           }
  104.                           else
  105.                              loc_puts("\007");
  106.                           break;
  107.  
  108.                      default:
  109.                           if (c < ' ' || c > 126)
  110.                              break;
  111.  
  112.                      case '\r':
  113.                      case '\a':
  114.                      case '\b':
  115.                           if (chattimer <= 0L)
  116.                              break;
  117.  
  118.                           chattimer = time(NULL) + CHAT_TIMEOUT;
  119.                           warned = false;
  120.  
  121.                           if (chatfill >= CHATLEN)
  122.                              loc_puts("\007");
  123.                           else {
  124.                              switch (c) {
  125.                                     case '\r':
  126.                                          curbuf[chatfill++] = '\n';
  127.                                          loc_puts("\r\n");
  128.                                          break;
  129.  
  130.                                     case '\b':
  131.                                          if (chatfill > 0 && curbuf[chatfill - 1] != '\n')
  132.                                             chatfill--;
  133.                                          else {
  134.                                             curbuf[chatfill++] = '\b';
  135.                                             curbuf[chatfill++] = ' ';
  136.                                             curbuf[chatfill++] = '\b';
  137.                                          }
  138.                                          loc_puts("\b \b");
  139.                                          break;
  140.  
  141.                                     default:
  142.                                          curbuf[chatfill++] = (byte) c;
  143.                                          if (c != 7)
  144. #if WIN_AGL
  145.                                             win_putc(local_win,c);
  146. #else
  147. #ifdef AMIGA
  148.                                             ConPrintf(LocalRequest,"%lc",c);
  149. #else
  150.                                             putch(c);
  151. #endif
  152. #endif
  153.                                          break;
  154.                              }
  155.                           }
  156.                           break;
  157.               }
  158.         }
  159.  
  160.         if (chatfill > 0 && hydra_devsend("CON",curbuf,chatfill)) {
  161.            curbuf = (curbuf == chatbuf1) ? chatbuf2 : chatbuf2;
  162.            chatfill = 0;
  163.         }
  164.  
  165.         return (esc);
  166. }/*keyabort()*/
  167.  
  168.  
  169. void rem_chat (byte *data, word len)
  170. {
  171.         len = len;
  172.  
  173. #if !WIN_AGL && !defined(AMIGA)
  174.         local_x = wherex();
  175.         local_y = wherey();
  176.         window(1,11,80,17);
  177.         gotoxy(remote_x,remote_y);
  178. #endif
  179.  
  180.         while (*data) {
  181.               switch (*data) {
  182.                      case '\a':
  183.                           if (!nobell) {
  184. #if WIN_AGL
  185.                              (void) win_bell();
  186. #else
  187. #ifdef AMIGA
  188.                              ConPrintf(RemoteRequest,"\a");
  189. #else
  190.                              putc(7,stderr);
  191. #endif
  192. #endif
  193.                           }
  194.                           break;
  195.  
  196.                      case '\n':
  197. #if WIN_AGL
  198.                           win_putc(remote_win,'\r');
  199. #else
  200. #ifdef AMIGA
  201.                           ConPrintf(RemoteRequest,"\r");
  202. #else
  203.                           putch('\r');
  204. #endif
  205. #endif
  206.                           /* fallthrough to default */
  207.  
  208.                      default:
  209. #if WIN_AGL
  210.                           win_putc(remote_win,(int) *data);
  211. #else
  212. #ifdef AMIGA
  213.                           ConPrintf(RemoteRequest,"%lc",*data);
  214. #else
  215.                           putch((int) *data);
  216. #endif
  217. #endif
  218.                           break;
  219.               }
  220.               data++;
  221.         }
  222.  
  223. #if !WIN_AGL && !defined(AMIGA)
  224.         remote_x = wherex();
  225.         remote_y = wherey();
  226.         window(1,19,80,25);
  227.         gotoxy(local_x,local_y);
  228. #endif
  229. }/*rem_chat()*/
  230.  
  231.  
  232. int parse(char *string)
  233. {
  234.         int ac = 0;
  235.         char *p;
  236.  
  237.         p = strchr(string,';');
  238.         if (p) *p = '\0';
  239. #ifndef AMIGA
  240.         strupr(string);
  241. #endif    /* AMIGA */
  242.         av[ac] = strtok(string," \t\r\n\032");
  243.  
  244.         while (av[ac]) {
  245.               if (++ac > MAXARGS) {
  246.                  message(6,"!Too many arguments!");
  247.                  endprog(2);
  248.               }
  249.               av[ac]=strtok(NULL," \t\r\n\032");
  250.         }
  251.  
  252.         return (ac);
  253. }/*parse()*/
  254.  
  255.  
  256. void splitpath(char *filepath,char *path,char *file)
  257. {
  258. #ifdef AMIGA
  259.     STRPTR path_name,file_name;
  260.     LONG len;
  261.  
  262.     path_name = PathPart(filepath);
  263.     file_name = FilePart(filepath);
  264.  
  265.     strcpy(file,file_name);
  266.  
  267.     len = path_name - filepath;
  268.  
  269.     memcpy(path,filepath,len);
  270.  
  271.     path[len] = 0;
  272. #else
  273.         char *p,*q;
  274.  
  275.         for (p=filepath;*p;p++) ;
  276.         while (p!=filepath && *p!=':' && *p!='\\' && *p!='/') --p;
  277.         if (*p==':' || *p=='\\' || *p=='/') ++p;        /* begin     */
  278.         q=filepath;
  279.         while (q!=p) *path++=*q++;                      /* copy path */
  280.         *path='\0';
  281.         strcpy(file,p);
  282. #endif    /* AMIGA */
  283. }/*splitpath()*/
  284.  
  285.  
  286. void mergepath(char *filepath,char *path,char *file)
  287. {
  288. #ifdef AMIGA
  289.     strcpy(filepath,path);
  290.     AddPart(filepath,file,256);
  291. #else
  292.         strcpy(filepath,path);
  293.         strcat(filepath,file);
  294. #endif    /* AMIGA */
  295. }/*mergepath()*/
  296.  
  297.  
  298. int fexist (char *filename)
  299. {
  300. #ifdef AMIGA
  301.     BPTR file_lock;
  302.  
  303.     if(file_lock = Lock(filename,SHARED_LOCK))
  304.     {
  305.         UnLock(file_lock);
  306.  
  307.         return(1);
  308.     }
  309.     else
  310.         return(0);
  311. #else
  312.         struct stat f;
  313.  
  314.         return ((stat(filename,&f) != -1) ? 1 : 0);
  315. #endif    /* AMIGA */
  316. }/*fexist()*/
  317.  
  318.  
  319. int get_key (void)
  320. {
  321. #ifdef AMIGA
  322.         return(ConGetKey());
  323. #else
  324. #if WIN_AGL
  325.         if (didsome)
  326.            return (win_keygetc());
  327.         else
  328. #endif
  329.              {
  330.            register int c = getch();
  331.  
  332.            return (c ? c : getch() | 0x100);
  333.         }
  334. #endif    /* AMIGA */
  335. }/*get_key()*/
  336.  
  337.  
  338. void any_key (void)
  339. {
  340.         fprintf(stderr,"Press any key to continue");
  341.         get_key();
  342.         fprintf(stderr,"\r                          \r");
  343. }/*any_key()*/
  344.  
  345.  
  346. int get_str (char *prompt, char *s, int maxlen)
  347. {
  348.         int i = (int) strlen(s),c;
  349.  
  350.         cprint("\r%s: %s",prompt,s);
  351.         for (;;) {
  352. #ifdef AMIGA
  353.             sys_idle();
  354.             if(!ConScanKey()) continue;
  355. #endif    /* AMIGA */
  356.             switch (c = get_key()) {
  357.                    case 13:
  358.                         s[i] = '\0';
  359.                         cprint("\n");
  360.                         return (i);
  361.  
  362.                    case 27:
  363.                         if (i) {
  364.                            do cprint("\b \b");
  365.                            while (--i);
  366.                         }
  367.                         s[0] = '\0';
  368.                         cprint("<aborted>\n");
  369.                         return (-1);
  370.  
  371.                    case 8:
  372.                    case 127:
  373.                         if (i) {
  374.                            --i;
  375.                            cprint("\b \b");
  376.                         }
  377.                         break;
  378.  
  379.                    default:
  380. #ifdef AMIGA
  381.                         if (i == maxlen || !((c >= 32 || c <= 126) && (c >= 160 || c <= 255))) {
  382. #else
  383.                         if (i == maxlen || c < 32 || c > 126) {
  384. #endif    /* AMIGA */
  385. #if WIN_AGL
  386.                            (void) win_bell();
  387. #else
  388. #ifdef AMIGA
  389.                            ConPrintf(LocalRequest,"\a");
  390. #else
  391.                            putc(7,stderr);
  392. #endif
  393. #endif
  394.                         }
  395.                         else {
  396.                            cprint("%c",c);
  397.                            s[i++] = c;
  398.                         }
  399.                         break;
  400.             }/*switch*/
  401.         }/*for*/
  402. }/*get_str()*/
  403.  
  404.  
  405. void resultlog (boolean xmit, char *fname, long bytes, long xfertime)
  406. {          /* Omen's DSZ compatible logfile - for RBBS-PC XFER-?.DEF reports */
  407.         FILE *fp;
  408.  
  409.         if (opuslog) {
  410.            if ((fp = sfopen(opuslog,"at",DENY_WRITE)) != NULL) {
  411.               if (fname) {
  412.                  fprintf(fp, "%s %s%s %ld", xmit ? "Sent" : "Got",
  413.                              xmit ? "" : download, fname, bytes);
  414.                  if (mailer)
  415.                     fprintf(fp," %ld",xfertime);
  416.                  fprintf(fp,"\n");
  417.               }
  418.               fclose(fp);
  419.            }
  420.            else
  421.               message(3,"-Couldn't append opus log-file %s",opuslog);
  422.         }
  423.  
  424.         if (result) {
  425.            if ((fp = sfopen(result,"at",DENY_WRITE)) != NULL) {
  426.               if (fname) {
  427.                  fprintf(fp, "%c %6ld %5u bps %4ld cps 0 errors     0 1024 %s -1\n",
  428.                              xmit ? 'H' : 'R',
  429.                              bytes, cur_speed,
  430.                              xfertime ? (bytes / xfertime) : 9999L,
  431.                              fname);
  432.               }
  433.               fclose(fp);
  434.            }
  435.            else
  436.               message(3,"-Couldn't append result-file %s",result);
  437.         }
  438. }/*resultlog()*/
  439.  
  440.  
  441. static char *mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  442.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  443.  
  444. char *h_revdate (long revstamp)
  445. {
  446.         static char  buf[12];
  447.         struct tm   *t;
  448.  
  449.         t = localtime(&revstamp);
  450.         sprintf(buf, "%02d %s %d",
  451.                      t->tm_mday, mon[t->tm_mon], t->tm_year + 1900);
  452.  
  453.         return (buf);
  454. }/*h_revdate()*/
  455.  
  456.  
  457. void message (int level, char *fmt,...)
  458. {
  459.         char       buf[255];
  460.         long       tim;
  461.         struct tm *t;
  462.         va_list    arg_ptr;
  463.  
  464.         tim = time(NULL);
  465.         t = localtime(&tim);
  466.  
  467.         va_start(arg_ptr,fmt);
  468.         sprintf(buf, "%c %02d %03s %02d:%02d:%02d %-4s ",
  469.                      *fmt, t->tm_mday, mon[t->tm_mon],
  470.                      t->tm_hour, t->tm_min, t->tm_sec, LOGID);
  471.         vsprintf(&buf[23], &fmt[1], arg_ptr);
  472.         va_end(arg_ptr);
  473.  
  474.         if (level >= loglevel && logfp)
  475.            fprintf(logfp, "%s\n", buf);
  476.  
  477. #if WIN_AGL
  478.         if (!file_win)
  479.            cprint("\r%s\n",buf);
  480.         else {
  481.            if (!log_first)
  482.               log_first = true;
  483.            else
  484.               win_putc(log_win,'\n');
  485.            win_puts(log_win,buf);
  486.         }
  487. #else
  488. #ifdef AMIGA
  489.         if (!LogRequest)
  490.            cprint("%s\n",buf);
  491.         else
  492.            ConPrintf(LogRequest,"%s\n",buf);
  493. #else
  494.         if (!file_x)
  495.            cprint("\r%s\n",buf);
  496.         else {
  497.            local_x = wherex();
  498.            local_y = wherey();
  499.            window(1,2,80,6);
  500.            if (log_y) {
  501.               gotoxy(1,log_y);
  502.               putch('\n');
  503.            }
  504.            cputs(buf);
  505.            log_y = wherey();
  506.            window(1,19,80,25);
  507.            gotoxy(local_x,local_y);
  508.         }
  509. #endif
  510. #endif
  511. }/*message()*/
  512.  
  513.  
  514. void cprint (char *fmt, ...)
  515. {
  516.         char    buf[255];
  517.         va_list arg_ptr;
  518.  
  519.         va_start(arg_ptr,fmt);
  520.         vsprintf(buf, fmt, arg_ptr);
  521.         va_end(arg_ptr);
  522.  
  523. #if WIN_AGL
  524.         if (didsome)
  525.            win_puts(0,buf);
  526.         else
  527. #else
  528. #ifdef AMIGA
  529.         if (LocalRequest)
  530.            ConPrintf(LocalRequest,buf);
  531.         else
  532. #endif
  533. #endif
  534.            fputs(buf,stdout);
  535. }/*cprint()*/
  536.  
  537.  
  538. void hydra_gotoxy (int x, int y)
  539. {
  540. #if WIN_AGL
  541.         win_setpos(file_win,x,y);
  542. #else
  543. #ifdef AMIGA
  544.         ConMove(FileRequest,x,y);
  545. #else
  546.         file_x = x;
  547.         file_y = y;
  548. #endif
  549. #endif
  550. }/*hydra_gotoxy()*/
  551.  
  552.  
  553. void hydra_printf (char *fmt, ...)
  554. {
  555.         char    buf[255];
  556.         va_list arg_ptr;
  557.  
  558.         va_start(arg_ptr,fmt);
  559.         vsprintf(buf, fmt, arg_ptr);
  560.         va_end(arg_ptr);
  561.  
  562. #if WIN_AGL
  563.         win_puts(file_win,buf);
  564. #else
  565. #ifdef AMIGA
  566.         ConPrintf(FileRequest,buf);
  567. #else
  568.         local_x = wherex();
  569.         local_y = wherey();
  570.         window(1,8,80,9);
  571.  
  572.         gotoxy(file_x,file_y);
  573.         cputs(buf);
  574.  
  575.         file_x = wherex();
  576.         file_y = wherey();
  577.         window(1,19,80,25);
  578.         gotoxy(local_x,local_y);
  579. #endif
  580. #endif
  581. }/*hydra_printf()*/
  582.  
  583.  
  584. void hydra_clreol (void)
  585. {
  586. #if WIN_AGL
  587.         win_clreol(file_win);
  588. #else
  589. #ifdef AMIGA
  590.         ConPrintf(FileRequest,"\33[K");
  591. #else
  592.         local_x = wherex();
  593.         local_y = wherey();
  594.         window(1,8,80,9);
  595.  
  596.         gotoxy(file_x,file_y);
  597.         clreol();
  598.  
  599.         file_x = wherex();
  600.         file_y = wherey();
  601.         window(1,19,80,25);
  602.         gotoxy(local_x,local_y);
  603. #endif
  604. #endif
  605. }/*hydra_clreol()*/
  606.  
  607. /* end of misc.c */
  608.